﻿global using Xunit;
using ApplicationBase.Core;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using IdentityService.Application;
using IdentityService.Application.Behaviours;
using IdentityService.Infrastructure;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;

namespace IdentityService.Test
{
    public class Startup
    {
        public void ConfigureHost(IHostBuilder hostBuilder) 
        {
            hostBuilder.UseServiceProviderFactory(new AutofacServiceProviderFactory());
            hostBuilder.ConfigureContainer<ContainerBuilder>(containerBuilder =>
            {
                containerBuilder.RegisterModule<BaseModule>();
                containerBuilder.RegisterModule<AppModule>();
            });

            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .WriteTo.File("log.txt", rollingInterval: RollingInterval.Day)
                .CreateLogger();
            hostBuilder.UseSerilog();
        }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext<AppIdentityDbContext>(options =>
            {
                //options.UseSqlServer("Server=.;Initial Catalog=IdentityService;Integrated Security=SSPI;Encrypt=True;TrustServerCertificate=True");
                options.UseSqlServer("Server=39.107.140.217,1433;Initial Catalog=IdentityService;User=sa;Password=Yez123!!;TrustServerCertificate=True;");
            });

            services.AddMediatR(cfg =>
            {
                cfg.RegisterServicesFromAssembly(typeof(TransactionBehaviour<,>).Assembly);
                cfg.AddOpenBehavior(typeof(PermitCheckBehaviour<,>));
                cfg.AddOpenBehavior(typeof(LoggerBehaviour<,>));
                cfg.AddOpenBehavior(typeof(TransactionBehaviour<,>));
            });
        }
    }
}
